import java.util.Scanner;
import java.lang.Number;

//计算X = Y^Z mod D
public class test1 {

    public static long LoopPower(long x, long n, long p)
    {
        if(n == 0)
            return 1;

        long tmp = LoopPower((x * x) % p, n / 2, p);

        if(n % 2 != 0)
            tmp = (tmp * x) % p;

        return tmp;
    }

    public static void main(String[] args) {
        long Y, Z, D;
        while (true)
        {
            System.out.println("Please Input Y, Z and D:(Input 'END' to STOP)");
            Scanner sc = new Scanner(System.in);
            String str  = sc.nextLine();
            if(str.equals("END"))
                break;
            else
            {
                String[] YZD = str.split(" ");
                Y = Long.parseLong(YZD[0]);
                Z = Long.parseLong(YZD[1]);
                D = Long.parseLong(YZD[2]);
                if (Y < 0 || Y > 2147483647 || Z < 0 || Z > 2147482647 || D < 1 || D >46340)
                {
                    System.out.println("Wrong number, please Input once again.");
                }
                else
                {
                    long result = LoopPower(Y, Z, D);
                    System.out.println(result);
                }
            }
        }

    }
}
